package com.example.park.weixiu

import android.content.Intent
import android.view.View
import android.widget.Adapter
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.WeiXiuEntityItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.lib_base.net.Const
import com.example.park.databinding.ActivityAddWeiXiuBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/park/AddWeiXiuActivity")
class AddWeiXiuActivity : BaseActivity<ActivityAddWeiXiuBinding, WeiXiuViewModel>() {
    var area = "A区停车场"
    val repairImgAdapter by lazy { RepairImgAdapter() }
    override fun initView() {
        binding.iv.setOnClickListener {
            finish()
        }
        binding.parkaddAdd.setOnClickListener {
            val phone = binding.repairaddPhone.text.toString()
            val remark = binding.repairaddRemark.text.toString()
            val user = binding.repairaddUser.text.toString()
            val currentTime = System.currentTimeMillis()
            val item = WeiXiuEntityItem(
                phone,
                remark,
                "",
                null,
                "WX${2025021023}",
                "待处理",
                area,
                user,
                System.currentTimeMillis().toString(),
                SPUtils.getInstance().getInt(Const.UID),
                false
            )
            vm.addWeiXiuXq(item)
        }
        val arr = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, arr)
        binding.repairaddLocarion.adapter = adapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.repairaddLocarion.onItemSelectedListener =
            object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                    area = arr[p2]
                }

                override fun onNothingSelected(p0: AdapterView<*>?) {

                }
            }
        binding.repairaddImg.setOnClickListener {
            if (repairImgAdapter.data.size < 4) {
                PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
            } else {
                ToastUtils.showLong("图片已达到上限")
            }
        }
        binding.repairaddRv.apply {
            this.adapter = repairImgAdapter
            layoutManager =
                LinearLayoutManager(this@AddWeiXiuActivity, RecyclerView.HORIZONTAL, false)
        }
    }

    override fun initData() {
        vm.addWeiXiuXqSuccess.observe(this) {
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            repairImgAdapter.addData(bean?.path!!)
            repairImgAdapter.notifyDataSetChanged()
        }
    }

}